package tech.yangzhijie.login.service.impl;

import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tech.yangzhijie.login.mapper.TUserMapper;
import tech.yangzhijie.login.pojo.TUser;
import tech.yangzhijie.login.pojo.TUserExample;
import tech.yangzhijie.login.service.UserService;
import tech.yangzhijie.login.vo.UserVo;


@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private TUserMapper tUserMapper;
	
	@Override
	public UserVo login(TUser tUser) {
		TUserExample example = new TUserExample();
		TUserExample.Criteria c = example.createCriteria();
		c.andUsernameEqualTo(tUser.getUsername());
		List<TUser> ulist = tUserMapper.selectByExample(example);
		if(ulist != null && !ulist.isEmpty()){
			TUser user = ulist.get(0);
			if(user.getPassword().equals(tUser.getPassword())){
				UserVo userVo = new UserVo();
				BeanUtils.copyProperties(user, userVo);
				return userVo;
			}
		}
		return null;
	}

	@Override
	public void saveUser(UserVo userVo) {
		tUserMapper.insert(userVo);
	}

	@Override
	public UserVo checkUsername(String username) {
		TUserExample example = new TUserExample();
		TUserExample.Criteria c = example.createCriteria();
		c.andUsernameEqualTo(username);
		List<TUser> list = tUserMapper.selectByExample(example);
		if(list.size()==0){
			return null;
		}else{
			UserVo vo = new UserVo();
			BeanUtils.copyProperties(list.get(0), vo);
			return vo;
		}
	}

}
